package com.ning.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ning.dao.ApplyDao;
import com.ning.pojo.Apply;
import com.ning.pojo.User;

@SuppressWarnings("unchecked")
public class ApplyDaoImpl extends HibernateDaoSupport implements ApplyDao {

	@Override
	public List<Apply> findApplyByUser(Integer userId) {
		String sql = "from Apply where user.id = "+userId;
		List<Apply> list = this.getHibernateTemplate().find(sql);
		return list;
	}

	@Override
	public void save(Apply apply) {
		this.getHibernateTemplate().save(apply);
	}

	@Override
	public List<Apply> findApplyByCompetent(Integer deptId) {
		String findDeptSql = "from User u where u.dept.id = "+deptId;
		List<User> userList = this.getHibernateTemplate().find(findDeptSql);
		String findApplySql = "";
		List<Apply> applyList = new ArrayList<Apply>();
		for(User user : userList){
			findApplySql = "from Apply a where a.user.id = "+user.getId()+"and a.state = '0' and a.flag='0'";
			List<Apply> list = this.getHibernateTemplate().find(findApplySql);
			applyList.addAll(list);
		}
		return applyList;
	}

	@Override
	public List<Apply> findApplyByManager() {
		String sql = "from Apply a where a.flag = '1'";
		List<Apply> list = this.getHibernateTemplate().find(sql);
		return list;
	}
	
	@Override
	public List<Apply> findApplyByBoss() {
		String sql = "from Apply a where a.flag = '2'";
		List<Apply> list = this.getHibernateTemplate().find(sql);
		return list;
	}

	@Override
	public Apply getApplyById(Integer applyId) {
		Apply apply_ = (Apply)this.getHibernateTemplate().get(Apply.class, applyId);
		return apply_;
	}

	@Override
	public void update(Apply apply) {
		this.getHibernateTemplate().update(apply);
	}


}
